package se.webbzon.boltzmann.minigame.question;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import se.webbzon.boltzmann.input.Input;
import se.webbzon.boltzmann.minigame.page.PageItem;
import se.webbzon.boltzmann.minigame.page.PageLayout;
import se.webbzon.boltzmann.minigame.page.PageParser;

public class QuestionSelector {
	
	// The layout of the generated questions
	private final PageLayout layout;
	
	// The input of the generated questions
	private final Input input;
	
	// The URLs of the question pages
	private final List<URL> urls;
	
	// The question listener
	private final QuestionListener listener;
	
	// A shuffled array of the selected questions
	private QuestionPage[] shuffled;
	
	// The index of the current question and the number of correct answers
	private int current, correct;
	
	/*============================================================
	Constructors
	============================================================*/

	public QuestionSelector(PageLayout layout, Input input, List<URL> pageURLs) {
		this.layout = layout;
		this.input = input;
		this.urls = pageURLs;
		listener = new QuestionListenerImpl();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Resets the question selector and resets the number of correct answers. **/
	public void reset() throws IOException {
		PageParser parser = new PageParser();
		parser.addTag(new AlternativeTag());
		parser.addTag(new AnswerTag());
		ArrayList<URL> urls = new ArrayList<URL>(this.urls);
		int questions = Math.min(20, urls.size());
		shuffled = new QuestionPage[questions];
		for (int i = 0; i < questions; i++) {
			final int index = (int) (urls.size() * Math.random());
			URL url = urls.remove(index);
			QuestionPage page = new QuestionPage(layout,input);
			for (PageItem item : parser.parse(layout, url)) {
				page.addItem(item);
			}
			shuffled[i] = page;
		}
		current = 0;
		correct = 0;
	}
	
	/** Returns the total number of questions selected by this selector. **/
	public int questions() {
		if (shuffled == null)
			return 0;
		else
			return shuffled.length;
	}
	
	/** Returns the index of the current question. **/
	public int question() {
		if (current > 0) {
			if (current < shuffled.length)
				return current-1;
			else
				return shuffled.length-1;
		} else
			return 0;
	}
	
	/** Returns true if there is a new question. **/
	public boolean hasNext() {
		return current < shuffled.length;
	}
	
	/** Return the next question page from this selector.  **/
	public QuestionPage next() {
		QuestionPage page = shuffled[current++];
		page.addListener(listener);
		return page;
	}
	
	/** Returns the current question page. **/
	public QuestionPage current() {
		if (current == 0)
			return null;
		else
			return shuffled[current-1];
	}
	
	/** Returns the number of correct answers. **/
	public int getCorrect() {
		return correct;
	}
	
	/** Sets the number of correct answers. **/
	public void setCorrect(int correct) {
		this.correct = correct;
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class QuestionListenerImpl implements QuestionListener {

		@Override public void onSubmit(QuestionPage page, boolean correct) {
			page.removeListener(listener);
			if (correct)
				setCorrect(getCorrect() + 1);
		}
		
	}

}
